home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / AOCE / Development Tools / Interfaces / CIncludes / DigitalSignature.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-10  |  15.3 KB  |  383 lines  |  [TEXT/MPS ]

  1. /************************************************************
  2.  
  3. Created: Tuesday, August 17, 1993 4:37:24 PM
  4.  DigitalSignature.h
  5.  C Interface to the Macintosh Libraries
  6.  
  7.   Copyright Apple Computer, Inc. 1990-1993
  8.   All rights reserved
  9.  
  10. ************************************************************/
  11.  
  12.  
  13. #ifndef __DIGITALSIGNATURE__
  14. #define __DIGITALSIGNATURE__
  15.  
  16. #ifndef __FILES__
  17. #include <Files.h>
  18. #endif
  19.  
  20. #ifndef __MEMORY__
  21. #include <Memory.h>
  22. #endif
  23.  
  24. #ifndef __TYPES__
  25. #include <Types.h>
  26. #endif
  27.  
  28.  
  29. /* —————————————————————————— MACROS AND TRAP SELECTORS —————————————————————————— */
  30. #ifndef __DigitalSignature
  31. #define __DigitalSignature(count,selector) = { 0x203C, count, selector, 0xAA5D}
  32. #endif
  33.  
  34. #define kSIGNewContext                    1900
  35. #define kSIGDisposeContext                1901
  36.  
  37. #define kSIGSignPrepare                    1902
  38. #define kSIGSign                        1903
  39.  
  40. #define kSIGVerifyPrepare                1904
  41. #define kSIGVerify                        1905
  42.  
  43. #define kSIGDigestPrepare                1906
  44. #define kSIGDigest                        1907
  45.  
  46. #define kSIGProcessData                    1908
  47.  
  48. #define kSIGShowSigner                    1909
  49. #define kSIGGetSignerInfo                1910
  50. #define kSIGGetCertInfo                    1911
  51. #define kSIGGetCertNameAttributes        1912
  52. #define kSIGGetCertIssuerNameAttributes    1913
  53.  
  54. #define kSIGFileIsSigned                2500
  55. #define kSIGSignFile                    2501
  56. #define kSIGVerifyFile                    2502
  57.  
  58.  
  59. /* values of SIGNameAttributeType */
  60. enum {
  61.     kSIGCountryCode,
  62.     kSIGOrganization,
  63.     kSIGStreetAddress,
  64.     kSIGState,
  65.     kSIGLocality,
  66.     kSIGCommonName,
  67.     kSIGTitle,
  68.     kSIGOrganizationUnit,
  69.     kSIGPostalCode
  70. };
  71.  
  72. typedef unsigned short SIGNameAttributeType;
  73.  
  74. /* 
  75. Certificate status codes returned in SIGCertInfo or SIGSignerInfo from
  76. either SIGGetCertInfo or SIGGetSignerInfo respectively. kSIGValid means that
  77. the certificate is currently valid. kSIGPending means the certificate is
  78. currently not valid - but will be.  kSIGExpired means the certificate has
  79. expired. A time is always associated with a SIGCertStatus.  In each case the
  80. time has a specific interpretation.  When the status is kSIGValid the time is
  81. when the certificate will expire. When the status is kSIGPending the time is
  82. when the certificate will become valid. When the status is kSIGExpired the time
  83. is when the certificate expired. In the SIGCertInfo structure, the startDate
  84. and endDate fields hold the appropriate date information.  In the SIGSignerInfo
  85. structure, this information is provided in the certSetStatusTime field. In the
  86. SIGSignerInfo struct, the status time is actually represented by the SIGSignatureStatus
  87. field which can contain any of the types below. NOTE: The only time you will get 
  88. a kSIGInvalid status is when it pertains to a SIGSignatureStatus field and only when
  89. you get a signature that was created after the certificates expiration date, something
  90. we are not allowing on the Mac but that may not be restricted on other platforms. Also, 
  91. it will not be possible to get a kSIGPending value for SIGSignatureStatus on the Mac but
  92. possibly allowed by other platforms.
  93. */
  94.  
  95. /* Values for SIGCertStatus or SIGSignatureStatus */
  96. enum {
  97.     kSIGValid,        /* possible for either a SIGCertStatus or SIGSignatureStatus */
  98.     kSIGPending,    /* possible for either a SIGCertStatus or SIGSignatureStatus */
  99.     kSIGExpired,    /* possible for either a SIGCertStatus or SIGSignatureStatus */
  100.     kSIGInvalid        /* possible only for a SIGSignatureStatus */
  101. };
  102.  
  103. typedef unsigned short SIGCertStatus;
  104. typedef unsigned short SIGSignatureStatus;
  105.  
  106. /* Gestalt selector code - returns toolbox version in low-order word */
  107. #define gestaltDigitalSignatureVersion 'dsig'
  108.  
  109.  
  110. /* Number of bytes needed for a digest record when using SIGDigest */
  111. #define kSIGDigestSize 16
  112.  
  113. typedef Byte SIGDigestData[kSIGDigestSize], *SIGDigestDataPtr;
  114.  
  115. struct SIGCertInfo {
  116.     unsigned long    startDate;                /* cert start validity date */
  117.     unsigned long    endDate;                /* cert end validity date */
  118.     SIGCertStatus    certStatus;                /* see comment on SIGCertStatus for definition */
  119.     unsigned long    certAttributeCount;        /* number of name attributes in this cert */
  120.     unsigned long    issuerAttributeCount;    /* number of name attributes in this certs issuer */
  121.     Str255            serialNumber;            /* cert serial number */
  122. };
  123.  
  124. typedef struct SIGCertInfo SIGCertInfo;
  125. typedef SIGCertInfo *SIGCertInfoPtr;
  126.  
  127. struct SIGSignerInfo {
  128.     unsigned long        signingTime;        /* time of signing */
  129.     unsigned long        certCount;            /* number of certificates in the cert set */
  130.     unsigned long        certSetStatusTime;    /* Worst cert status time. See comment on SIGCertStatus for definition */
  131.     SIGSignatureStatus    signatureStatus;    /* The status of the signature. See comment on SIGCertStatus for definition*/
  132. };
  133.  
  134. typedef struct SIGSignerInfo SIGSignerInfo;
  135. typedef SIGSignerInfo *SIGSignerInfoPtr;
  136.  
  137. struct SIGNameAttributesInfo {
  138.     Boolean                 onNewLevel;
  139.     SIGNameAttributeType    attributeType;
  140.     ScriptCode                attributeScript;
  141.     Str255                    attribute;
  142. };
  143.  
  144. typedef struct SIGNameAttributesInfo SIGNameAttributesInfo;
  145. typedef SIGNameAttributesInfo *SIGNameAttributesInfoPtr;
  146.  
  147. typedef Ptr    SIGContextPtr;
  148. typedef Ptr SIGSignaturePtr;
  149.  
  150. /*
  151. Certificates are always in order. That is, the signers cert is always 0, the
  152. issuer of the signers cert is always 1 etc… to the number of certificates-1.
  153. You can use this constant for readability in your code.
  154. */
  155.  
  156. #define kSIGSignerCertIndex 0
  157.  
  158. /*
  159. Call back procedure supplied by developer, return false to cancel the current
  160. process.
  161. */
  162.  
  163. typedef pascal Boolean (*SIGStatusProcPtr)(void);
  164.  
  165. /*
  166. Resource id's of standard signature icon suite, all sizes and colors are available.
  167. */
  168.  
  169. #define kSIGSignatureIconResID             -16797
  170. #define kSIGValidSignatureIconResID     -16799
  171. #define kSIGInvalidSignatureIconResID     -16798
  172.  
  173. /* ——————————————————————————————— CONTEXT CALLS ——————————————————————————————— */
  174. /*
  175. To use the Digital Signature toolbox you will need a SIGContextPtr.  To create
  176. a SIGContextPtr you simply call SIGNewContext and it will create and initialize
  177. a context for you.  To free the memory occupied by the context and invalidate
  178. its internal data, call SIGDisposeContext. An initialized context has no notion
  179. of the type of operation it will be performing however, once you call
  180. SIGSignPrepare SIGVerifyPrepare, or SIGDigestPrepare, the contexts operation
  181. type is set and to switch  to another type of operation will require creating a
  182. new context. Be sure to pass the same context to corresponding toolbox calls
  183. (ie SIGSignPrepare, SIGProcessData, SIGSign)  in other words mixing lets say
  184. signing and verify calls with the same context is not allowed.
  185. */
  186.  
  187. #ifdef __cplusplus
  188. extern "C" {
  189. #endif
  190.  
  191. pascal OSErr SIGNewContext(SIGContextPtr *context)
  192.     __DigitalSignature(2, kSIGNewContext);
  193.  
  194. pascal OSErr SIGDisposeContext(SIGContextPtr context)
  195.     __DigitalSignature(2, kSIGDisposeContext);
  196.  
  197.  
  198. /* ——————————————————————————————— SIGNING CALLS ——————————————————————————————— */
  199. /*
  200. Once you have created a SIGContextPtr, you create a signature by calling
  201. SIGSignPrepare once, followed by n calls to SIGProcessData, followed by one call
  202. toRcpt SIGSign. To create another signature on different data but for the same
  203. signer, don't dispose of the context and call SIGProcessData for the new data
  204. followed by a call SIGSign again. In this case the signer will not be prompted
  205. for their signer and password again as it was already provided.  Once you call
  206. SIGDisposeContext, all signer information will be cleared out of the context and
  207. the signer will be re-prompted.  The signer file FSSpecPtr should be set to nil
  208. if you want the toolbox to use the last signer by default or prompt for a signer
  209. if none exists.  The prompt parameter can be used to pass a string to be displayed
  210. in the dialog that prompts the user for their password.  If the substring "^1"
  211. (without the quotes) is in the prompt string, then the toolbox will replace it
  212. with the name of the signer from the signer selected by the user.  If an empty
  213. string is passed, the following default string will be sent to the toolbox
  214. "\pSigning as ^1.".  You can call any of the utility routines after SIGSignPrepare
  215. or SIGSign to get information about the signer or certs.
  216. */
  217.  
  218. pascal OSErr SIGSignPrepare(SIGContextPtr context, const FSSpec *signerFile,
  219.     ConstStr255Param prompt, Size *signatureSize)
  220.     __DigitalSignature(8, kSIGSignPrepare);
  221.  
  222. pascal OSErr SIGSign(SIGContextPtr context, SIGSignaturePtr signature,
  223.     SIGStatusProcPtr statusProc)
  224.     __DigitalSignature(6, kSIGSign);
  225.  
  226.  
  227. /* ——————————————————————————————— VERIFYING CALLS ——————————————————————————————— */
  228. /*
  229. Once you have created a SIGContextPtr, you verify a signature by calling
  230. SIGVerifyPrepare  once, followed by n calls to SIGProcessData, followed by one
  231. call to SIGVerify. Check the return code from SIGVerify to see if the signature
  232. verified or not (noErr is returned on  success otherwise the appropriate error
  233. code).  Upon successfull verification, you can call any of the utility routines
  234. toRcpt find out who signed the data.
  235. */
  236.  
  237. pascal OSErr SIGVerifyPrepare(SIGContextPtr context, SIGSignaturePtr signature,
  238.     Size signatureSize, SIGStatusProcPtr statusProc)
  239.     __DigitalSignature(8, kSIGVerifyPrepare);
  240.  
  241. pascal OSErr SIGVerify(SIGContextPtr context)
  242.     __DigitalSignature(2, kSIGVerify);
  243.  
  244. /* —————————————————————————————— DIGESTING CALLS —————————————————————————————— */
  245. /*
  246. Once you have created a SIGContextPtr, you create a digest by calling
  247. SIGDigestPrepare once, followed by n calls to SIGProcessData, followed by one
  248. call to SIGDigest.  You can dispose of the context after SIGDigest as the
  249. SIGDigestData does not reference back into it.  SIGDigest returns the digest in
  250. digest.
  251. */
  252.  
  253. pascal OSErr SIGDigestPrepare(SIGContextPtr context)
  254.     __DigitalSignature(2, kSIGDigestPrepare);
  255.  
  256. pascal OSErr SIGDigest(SIGContextPtr context, SIGDigestData digest)
  257.     __DigitalSignature(4, kSIGDigest);
  258.  
  259.  
  260. /* —————————————————————————————— PROCESSING DATA —————————————————————————————— */
  261. /*
  262. To process data during a digest, sign, or verify operation call SIGProcessData
  263. as many times as necessary and with any sized blocks of data.  The data needs to
  264. be processed in the same order during corresponding sign and verify operations
  265. but does not need to be processed in the same sized chunks (i.e., the toolbox
  266. just sees it as a continuous bit stream).
  267. */
  268.  
  269. pascal OSErr SIGProcessData(SIGContextPtr context, const void *data, Size dataSize)
  270.     __DigitalSignature(6, kSIGProcessData);
  271.  
  272.  
  273. /* ——————————————————————————————— UTILITY CALLS ——————————————————————————————— */
  274. /*
  275. Given a context that has successfully performed a verification SIGShowSigner
  276. will  display a modal dialog with the entire distinguished name of the person
  277. who signed the data. the prompt (if supplied) will appear at the top of the
  278. dialog.  If no prompt is specified, the default prompt "\pVerification
  279. Successfull." will appear.
  280.  
  281. Given a context that has been populated by calling SIGSignPrepare, SIGSign or a
  282. successful SIGVerify, you can make the remaining utility calls:
  283.  
  284. SIGGetSignerInfo will return the SignerInfo record.  The certCount can be used
  285. toRcpt index into the certificate set when calling SIGGetCertInfo,
  286. SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes. The signingTime is
  287. only defined if the call is made after SIGSign  or SIGVerify. The certSetStatus
  288. will tell you the best status of the entire certificate set while
  289. certSetStatusTime will correspond to the time associated with that status (see
  290. definitions above).
  291.  
  292. SIGGetCertInfo will return the SIGCertInfo record when given a valid index into
  293. the cert set in  certIndex.  Note: The cert at index kSIGSignerCertIndex is
  294. always the signers certificate.  The  serial number, start date and end date
  295. are there should you wish to display that info.  The  certAttributeCount and
  296. issuerAttributeCount provide the number of parts in the name of that certificate
  297. or that certificates issuer respectively.  You use these numbers to index into
  298. either SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes to retrieve
  299. the name. The certStatus will tell you the status of the certificate while
  300. certStatusTime will correspond to the time associated with that status (see
  301. definitions above).
  302.  
  303. SIGGetCertNameAttributes and SIGGetCertIssuerNameAttributes return name parts
  304. of the certificate at  certIndex and attributeIndex.  The newLevel return value
  305. tells you wether the name attribute returned is at the same level in the name
  306. hierarchy as the previous attribute.  The type return value tells you  the type
  307. of attribute returned. nameAttribute is the actual string containing the name
  308. attribute.   So, if you wanted to display the entire distinguished name of the
  309. person who's signature was just validated you could do something like this;
  310.  
  311.     (…… variable declarations and verification code would preceed this sample ……)
  312.  
  313.     error = SIGGetCertInfo(verifyContext, kSIGSignerCertIndex, &certInfo);
  314.     HandleErr(error);
  315.  
  316.     for (i = 0; i <= certInfo.certAttributeCount-1; i++)
  317.         {
  318.         error = SIGGetCertNameAttributes(
  319.             verifyContext, kSIGSignerCertIndex, i, &newLevel, &type, theAttribute);
  320.         HandleErr(error);
  321.         DisplayNamePart(theAttribute, type, newLevel);
  322.         }
  323. */
  324.  
  325. pascal OSErr SIGShowSigner(SIGContextPtr context, ConstStr255Param prompt)
  326.     __DigitalSignature(4, kSIGShowSigner);
  327.  
  328. pascal OSErr SIGGetSignerInfo(SIGContextPtr context, SIGSignerInfo *signerInfo)
  329.     __DigitalSignature(4, kSIGGetSignerInfo);
  330.  
  331. pascal OSErr SIGGetCertInfo(SIGContextPtr context, unsigned long certIndex, SIGCertInfo *certInfo)
  332.     __DigitalSignature(6, kSIGGetCertInfo);
  333.  
  334. pascal OSErr SIGGetCertNameAttributes(SIGContextPtr context, unsigned long certIndex, 
  335.                                       unsigned long attributeIndex, SIGNameAttributesInfo *attributeInfo)
  336.     __DigitalSignature(8, kSIGGetCertNameAttributes);
  337.  
  338. pascal OSErr SIGGetCertIssuerNameAttributes(SIGContextPtr context, unsigned long certIndex, 
  339.                                             unsigned long attributeIndex, SIGNameAttributesInfo *attributeInfo)
  340.     __DigitalSignature(8, kSIGGetCertIssuerNameAttributes);
  341.  
  342.  
  343.  
  344. /* ——————————————————————————— FILE SIGN & VERIFY CALLS —————————————————————————— */
  345. /* 
  346. These calls allow you to detect the presence of a standard signtaure in a file as 
  347. well as sign and verify files in a standard way.  An example of this is the Finder, 
  348. which uses these calls to allow the user to "drop sign" a file.
  349.  
  350. To detect if a file is signed in the standard way, pass the FSSpec of the file to SIGFileIsSigned.
  351. A result of noErr means the file is in fact signed, otherwise, a kSIGNoSignature error will
  352. be returned.
  353.  
  354. Once you have created a SIGContextPtr, you can make calls to either sign or verify a file in
  355. a standard way: 
  356.  
  357. To sign a file, call SIGSignPrepare followed by 'n' number of calls to SIGSignFile,
  358. passing it the file spec for each file you wish to sign in turn.  You supply the context, the signature 
  359. size that was returned from SIGSignPrepare and an optional call back proc.  The call will take care of all
  360. the processing of data and affixing the signature to the file. If a signature already exists in the file, 
  361. it is replaced with the newly created signature.
  362.  
  363. To verify a file that was signed using SIGSignFile, call SIGVerifyFile passing it a new context and 
  364. the file spec.  Once this call has completed, if the verification is successfull, you can pass the context 
  365. to SIGShowSigner to display the name of the person who signed the file.
  366. */
  367.  
  368. pascal OSErr SIGFileIsSigned(const FSSpec *fileSpec) 
  369.     __DigitalSignature(2, kSIGFileIsSigned);
  370.  
  371. pascal OSErr SIGSignFile(SIGContextPtr context, Size signatureSize, const FSSpec *fileSpec, SIGStatusProcPtr statusProc) 
  372.     __DigitalSignature(8, kSIGSignFile);
  373.  
  374. pascal OSErr SIGVerifyFile(SIGContextPtr context, const FSSpec *fileSpec, SIGStatusProcPtr statusProc)
  375.     __DigitalSignature(6, kSIGVerifyFile);
  376.  
  377. #ifdef __cplusplus
  378. }
  379. #endif
  380.  
  381.  
  382. #endif
  383.